package org.nokatag.system;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Date;

/**
 * <p>@Title 设置参数</p>
 * <p>@Description </p>
 * <p>@Version 1.0.0</p>
 * <p>@author rebin</p>
 * <p>@date 2016年12月17日</p>
 * <p>xiefangjian@163.com</p>
 * <p>Copyright © Noka Laboratory.All Rights Reserved.</p>
 */
public class PreparedParemSet {
	public  void setPre(PreparedStatement pa,Integer indx,Object o,int types) throws SQLException{
		switch(types){
		case Types.CHAR:
		case Types.VARCHAR:
		case Types.LONGVARCHAR:
			if(o instanceof String){
				pa.setString(indx, String.valueOf(o));
			}else if(o instanceof InputStream){
				pa.setString(indx,new String(toByteArray((InputStream)o)));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.BINARY:
		case Types.VARBINARY:
		case Types.LONGVARBINARY:
			if(o instanceof InputStream){
				pa.setBytes(indx,toByteArray((InputStream)o));
			}else if(o instanceof ByteArrayOutputStream){
				pa.setBytes(indx,((ByteArrayOutputStream)o).toByteArray());
			}else if(o instanceof String){
				pa.setBytes(indx, String.valueOf(o).getBytes());
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.TINYINT:
		case Types.SMALLINT:
			if(o instanceof Short){
				pa.setShort(indx,(Short)o);
			}else if(o instanceof Byte){
				pa.setByte(indx,(Byte)o);
			}else if(o instanceof Integer){
				pa.setInt(indx, (Integer)o);
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.INTEGER:
			if(o instanceof Integer){
				pa.setInt(indx,(Integer)o);
			}else if(o instanceof String){
				pa.setInt(indx,Integer.parseInt(String.valueOf(o)));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.BIGINT:
			if(o instanceof Long){
				pa.setLong(indx,(Long)o);
			}else if(o instanceof String){
				pa.setLong(indx,Long.parseLong(String.valueOf(o)));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.DOUBLE:
			if(o instanceof Double){
				pa.setDouble(indx,(Double)o);
			}else if(o instanceof String){
				pa.setDouble(indx,Double.parseDouble((String.valueOf(o))));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.REAL:
		case Types.FLOAT:
			if(o instanceof Float){
				pa.setFloat(indx,(Float)o);
			}else if(o instanceof String){
				pa.setFloat(indx,Float.parseFloat(String.valueOf(o)));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.DECIMAL:
		case Types.NUMERIC:
			if(o instanceof BigDecimal){
				pa.setBigDecimal(indx,(BigDecimal)o);
			}else if(o instanceof String){
				pa.setString(indx,String.valueOf(o));
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.DATE:
			if(o instanceof Date){
				pa.setDate(indx,new java.sql.Date(((Date) o).getTime()));
			}else if(o instanceof java.sql.Date){
				pa.setDate(indx,(java.sql.Date)o);
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.TIME:
			if(o instanceof java.sql.Time){
				pa.setTime(indx, (java.sql.Time)o);
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.TIMESTAMP:
			if(o instanceof java.sql.Timestamp){
				pa.setTimestamp(indx, (java.sql.Timestamp)o);
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		case Types.CLOB:
		case Types.BLOB:
			if(o instanceof String){
				byte[] byts = String.valueOf(o).getBytes();
				InputStream is=new ByteArrayInputStream(byts);
				pa.setBinaryStream(indx, is,byts.length);
			}else if(o instanceof InputStream){
				byte[] byts = toByteArray((InputStream)o);
				InputStream is=new ByteArrayInputStream(byts);
				pa.setBinaryStream(indx, is,byts.length);
			}else{
				pa.setObject(indx, o,types);
			}
			break;
		default:
			pa.setObject(indx, o,types);
		}
			
	}
	private  byte[] toByteArray(InputStream input){
		ByteArrayOutputStream output = new ByteArrayOutputStream();
		try{
		    byte[] buffer = new byte[1024];
		    int n = 0;
		    while (-1 != (n = input.read(buffer))) {
		        output.write(buffer, 0, n);
		    }
		}catch(Exception se){
			BugInfoWrint.Print(se);
		}
	    return output.toByteArray();
	}
	
}
